package ru.ntcreb.csir.gui;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import ru.ntcreb.csir.libs.methods.SVCH.SVCHService;
import ru.ntcreb.csir.libs.methods.SVCH.TractService;
import ru.ntcreb.csir.libs.methods.SVCH.dto.SVCHImpulseDTO;
import ru.ntcreb.csir.libs.methods.SVCH.dto.TractDTO;

public class Main extends Application {

	
	public void createChart(final LineChart<Number, Number> lineChart){
		
		
		TractDTO tract = new TractDTO();
		tract.setApproximationCoefficient_K(0.7);
		tract.setApproximationCoefficient_N(0.5);
		tract.setStepness_S(0.005);
		tract.setVoltage_U0(0);

		TractService tr = new TractService();
		TractDTO tract1 = tr.createTractDTO(0.7, 0.5, 0.005, 0.009);
		TractDTO tract2 = tr.createTractDTO(0.3, 0.35, 0, 0.009);

		double[] result2 = tr.createTractArray(tract1);
		double[] result = tr.createTractArray(tract2);
		
		//test
		double[] result1 = tr.createParallelTwoTractsArray(tract, tract2);

		double[] result3 = tr.createConsistentTwoTractsArray(tract, tract2);

		SVCHService svchServ = new SVCHService();

		SVCHImpulseDTO imp = svchServ.createSVCHImpulseDTO(500000, 60000,
				70000, 800000);
		double[] result4 = svchServ.createSVCHImpulse(imp);

		double[] result5 = svchServ.createTractSVCHReaction(result1, tract);
////
		
		XYChart.Series series = new XYChart.Series();

		XYChart.Series series2 = new XYChart.Series();
		

		double j = 0.1;
		for (int i = 0; i < result.length; i++) {
			series2.getData().add(new XYChart.Data(j, result4[i]));
			//series.getData().add(new XYChart.Data(j, result[i]));
			j += 0.1;
		}
		
		lineChart.getData().addAll(series,series2);
	}
	
	@Override
	public void start(Stage stage){

		

		final Group root = new Group();

		stage.setTitle("Microwave analyzer");		
		
		
		Scene scene = new Scene(root, 800, 600);
		
		final NumberAxis xAxis = new NumberAxis();
		final NumberAxis yAxis = new NumberAxis();
		xAxis.setLabel("U");
		yAxis.setLabel("I");
		
		final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(
				xAxis, yAxis);

		lineChart.setTitle("Tract");
		
		Button button1 = new Button();
		button1.setText("Create Tract");
		button1.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent arg0) {
				createChart(lineChart);
				
			}
		});
		
		root.getChildren().addAll(lineChart,button1);
		stage.setScene(scene);
		stage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
